/**
 * 投标信息申请
 * @author miaoyl
 * @date 2018/01/14
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        vueData = {
            activeName: 'first'
        },
        table_head_where = {},
        table_detail_where = {
            gwal001: ''
        },
        table_where = {
            GWGCID: ''
        },
        cols = [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'GWGC001', width: 160, title: '招标编号', fixed: 'left', sort: true,
                search: 'default',searchOpts: {icon:true}}
            , {field: 'GWGC022', width: 160, title: '项目名称',search: 'default'}
            , {field: 'GWGC023_NAME', width: 160, title: '客户名称', sort:true}
            , {field: 'GWGC024', width: 160, title: '开标时间', sort:true}
            , {field: 'GWGC002', width: 160, title: '保证金截止时间', sort:true}
            , {field: 'GWGC004', width: 160, title: '是否授权',filterDatas: app.datas_('sys_n_y')}
            , {field: 'GWGC005_NAME', width: 160, title: '授权代理商',search: 'default',searchOpts: {icon:true}}
            , {field: 'GWGC006', width: 160, title: '是否新增',filterDatas: app.datas_('sys_n_y')}
            , {field: 'GWGC020', width: 160, title: '备注'}
            , {field: 'GWGCACTI', width: 100, title: '状态',filterDatas: app.datas_('gwgcacti')}
            ,{
                width: 60,
                title: '',
                toolbar: '#script-gwgi002setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwgi002SettingAction,

        gwgd_cols = [[
            {field: 'GWGD003', width: 80, title: '标段号'}
            , {field: 'GWGC002', width: 120, title: '保证金截止时间'}
            , {field: 'GWGD006', width: 140, title: '产品编号'}
            , {field: 'GWAHL003', width: 80, title: '产品大类'}
            , {field: 'GWAOL003', width: 80, title: '车辆类型'}
            , {field: 'GWGD007', width: 80, title: '数量'}
            // , {field: 'GWGC008', width: 80, title: '样车申请',filterDatas: app.datas_('sys_n_y')}
            , {field: 'GWGC014', width: 80, title: '是否中标',filterDatas: app.datas_('sys_n_y')}
            , {field: 'GWGC015', width: 80, title: '中标单位'}
            , {field: 'GWGD010', width: 80, title: '中标单价'}
            , {field: 'NUM_SUM', width: 80, title: '中标总价'}
        ]],

        gwal_cols = [[
            {field: 'GWGE002', width: 80, title: '序号'}
            , {field: 'GWGE003', width: 120, title: '标段号'}
            // , {field: 'GWGE004', width: 120, title: '车辆型号'}
            , {field: 'GWGE005', width: 140, title: '产品编号'}
            , {field: 'GWAHL003', width: 80, title: '产品大类'}
            , {field: 'GWAOL003', width: 80, title: '车辆类型'}
            , {field: 'GWGE006', width: 100, title: '投标单位'}
            , {field: 'GWGE007', width: 100, title: '数量'}
            , {field: 'GWGE008', width: 100, title: '单价'}
            , {field: 'GWGE009', width: 100, title: '总计'}
            , {field: 'GWGE010', width: 100, title: '是否中标',
                filterDatas: app.datas_('sys_n_y')
            }
        ]],
        moduleName = '投标信息申请'
    ;

    var Gwgi002 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwgi002',
                _searchs: [{
                    id: 'gwgi002', tableId: 'gwgi002_table', datas: _searchsArr.pushArray([
                        {
                            field: 'GWGA008', title: '客户',
                            search: 'default',searchOpts: {icon:true}
                        },{
                             field: 'GWGC014', title: '是否中标', search: 'default',
                             filterDatas: app.datas_('sys_n_y'), searchOpts: {type: 'select'}
                         },{
                            field: 'GWGCORIU', title: '创建人',
                            search: 'default',searchOpts: {icon:true}
                        }
                    ])
                }],
                _loadTplsFull: [
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    },
                    {
                        dom: 'gwgi002_edit', tpl: 'gwgi002_edit'
                    },
                    {
                        dom: 'gwgiBookmark', tpl: 'gwgiBookmark',type:"child",target:"gwgiBookmark"
                    },
                    {
                        dom: 'gwglBookmark', tpl: 'gwglBookmark',type:"child",target:"gwglBookmark"
                    },
                    {
                        dom: 'gwcaBookmark', tpl: 'gwcaBookmark',type:"child",target:"gwcaBookmark"
                    }
                ]
            }, win);
        },

        initHeadTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwgi002_table'),
                    url: g_ctxPath + 'crm/gwgi002/getGwgclist',
                    exportsUrl: g_ctxPath + 'crm/gwgi002/getGwgclist',
                    exportsName: '招标申请数据',
                    title: '招标申请信息表',
                    method: 'post',
                    id: 'gwgi002_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwgi002setting-action').innerHTML).render({}, function (string) {
                    gwgi002SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwgi002_table)', function (obj) {

                    layer.tips(gwgi002SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        if(obj.data.GWGCACTI === 'Y' || obj.data.GWGCACTI === "T"){
                                            app.showWarning("已审核或已提交资料不可编辑");
                                            return;
                                        }
                                        app.ajax({
                                            xurl: 'crm/gwgi002/getGwgeCount',
                                            xdata: obj.data,
                                            cb: function (result) {
                                                if (result.ok) {
                                                    if(result.data.data.TOTAL == 0){
                                                        self._openTplFull({
                                                            tag: 'gwgi002_edit',
                                                            tagData: {
                                                                obj: obj
                                                            },
                                                            index: 1,
                                                            openOpts: {
                                                                title: moduleName + '编辑',
                                                                maxmin: true
                                                            }
                                                        });
                                                    }else {
                                                        app.showWarning("已有中标信息，不可维护！");
                                                        return;
                                                    }
                                                }
                                            }
                                        });
                                        break;
                                    case '1':
                                        if(obj.data.GWGCACTI == 'Y'){
                                            app.showWarning("已审核状态不可删除！");
                                            return;
                                        }
                                        self.delData(obj);
                                        break;
                                    case '2':
                                        /*if(obj.data.GWGCACTI === 'Y' && obj.data.GWGCACTI !== "T"){
                                            app.showWarning("已审核或已提交资料不可编辑");
                                            return;
                                        }*/
                                        self._openTplFull({
                                            tag: 'gwgi002_edit',
                                            tagData: {
                                                obj: obj,
                                                is_addGwde:true
                                            },
                                            index: 1,
                                            openOpts: {
                                                title: moduleName + '中标信息编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行单击事件
                table.on('row(gwgi002_table)', function (obj) {
                    table_where.GWGCID = obj.data.GWGCID;
                    obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                    self.reloadGwgdTableCurr();
                    self.reloadGwgeTableCurr();
                    self.reloadGwgiTableCurr();
                    self.reloadGwglTableCurr();
                    self.reloadGwcaTableCurr();
                });

                //监听行双击事件
                table.on('rowDouble(gwgi002_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwgi002_edit',
                        tagData: {
                            obj: obj,
                            is_detail:true
                        },
                        index: 1,
                        openOpts: {
                            title: moduleName + '详情',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwgi002_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwgi002_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 1,
                                openOpts: {
                                    title: moduleName + '编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            self.delDataBatch();
                            break;
                        case 'submit' :
                            self.changeStatusBatch("submit");
                            break;
                        case 'cancelSubmit' :
                            self.changeStatusBatch("cancelSubmit");
                            break;
                        case 'check' : // 审核
                            self.changeStatusBatch("check");
                            break;
                        case 'revoke' : // 退回
                            self.changeStatusBatch("revoke");
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        initGwgdTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwgi002_table_gwgd'),
                    url: g_ctxPath + 'crm/gwgi002/getGwgdList',
                    exportsUrl: g_ctxPath + 'crm/gwgi002/getGwgdList',
                    exportsName: '投标车型明细数据',
                    title: '投标车型明细信息表',
                    method: 'post',
                    id: 'gwgi002_table_gwgd',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    /*toolbar: '#detail-table-toolbar',
                    defaultToolbar: ['filter', 'exports'],*/
                    height: '400px',
                    cols: gwgd_cols,
                    page: true,
                    where: table_where,
                    done: function (res, curr, count) {

                    }
                });
            });
        },

        initGwgeTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwgi002_table_gwge'),
                    url: g_ctxPath + 'crm/gwgi002/getGwgeList',
                    exportsUrl: g_ctxPath + 'crm/gwgi002/getGwgeList',
                    exportsName: '投标反馈数据',
                    title: '投标反馈信息表',
                    method: 'post',
                    id: 'gwgi002_table_gwge',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    /*toolbar: '#detail-table-toolbar',
                     defaultToolbar: ['filter', 'exports'],*/
                    height: '400px',
                    cols: gwal_cols,
                    page: true,
                    where: table_where,
                    done: function (res, curr, count) {

                    }
                });
            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwgi002_table', where: table_head_where});
        },

        reloadGwgdTableCurr: function () {
            self._reloadTable({id: 'gwgi002_table_gwgd', where: table_where});
        },

        reloadGwgeTableCurr: function () {
            self._reloadTable({id: 'gwgi002_table_gwge', where: table_where});
        },

        reloadGwgiTableCurr: function () {
            self._reloadTable({id: 'gwgiBookmark', where: table_where});
        },

        reloadGwglTableCurr: function () {
            self._reloadTable({id: 'gwglBookmark', where: table_where});
        },

        reloadGwcaTableCurr: function () {
            self._reloadTable({id: 'gwcaBookmark', where: table_where});
        },

        delData: function (obj) {
            if (obj.data.GWGCACTI !== 'N' && obj.data.GWGCACTI !== 'R') {
                app.showWarning("只有未审核或拒审的资料才能删除");
                return;
            }
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwgi002/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: moduleName + '删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                                self.reloadGwgdTableCurr();
                                self.reloadGwgeTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwgi002_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if (v.GWGCACTI !== 'N' && v.GWGCACTI !== 'R') {
                        app.showWarning("只有未审核或拒审的资料才能删除！");
                        flag = false;
                    }else {
                        var _temp = app.fields_('GWGC_T');
                        _temp.GWGCID = v.GWGCID;
                        _temp.GWGCENT = v.GWGCENT;
                        _xdata.push(_temp);
                    }
                });
                if(flag) {
                    app.ajax({
                        xurl: 'crm/gwgi002/delDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm:{msg: "是否删除选择的数据？"},
                        okmsg: moduleName + '删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                                self.reloadGwgdTableCurr();
                                self.reloadGwgeTableCurr();
                            }
                        }
                    });
                }
            }
        },

        changeStatusBatch:function(action) {
            var checkStatus = table.checkStatus('gwgi002_table');
            var flag = true;
            if(checkStatus.data.length === 0) {
                app.showWarning("至少选择一笔数据进行操作");
            }
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    switch (action) {
                        case "submit" :  // 提交
                            if( v.GWGCACTI === "N" || v.GWGCACTI === 'R'){
                                v.GWGCACTI = "T"
                            }
                            else {
                                app.showWarning("未审核或退回状态，才能进行提交操作");
                                flag = false;
                                return;
                            }
                            break;
                        case "cancelSubmit" : // 取消提交
                            if(v.GWGCACTI === "T"){
                                v.GWGCACTI = "N"
                            }
                            else {
                                app.showWarning("已提交的资料才能取消提交");
                                flag = false;
                                return;
                            }
                            break;
                        case "check" : // 审核
                            if(v.GWGCACTI === "T"){
                                v.GWGCACTI = "Y"
                            }
                            else {
                                app.showWarning("已提交的资料才能审核");
                                flag = false;
                                return;
                            }
                            break;
                        case "revoke" : // 退回
                            if(v.GWGCACTI === "T"){
                                v.GWGCACTI = "R"
                            }
                            else {
                                app.showWarning("已提交的资料才能退回");
                                flag = false;
                                return;
                            }
                            break;
                    }
                    _xdata.push({
                        GWGCID: v.GWGCID,
                        GWGCACTI: v.GWGCACTI,
                        GWGCENT: v.GWGCENT,
                        GWGC021: v.GWGC021
                    });
                });
                if(!flag){
                    return;
                }else {
                    app.ajax({
                        xurl: 'crm/gwgi002/updateStatus',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: moduleName +'状态修改成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        checkProcess:function() {
            var checkStatus = table.checkStatus('gwgi002_table');
            if(checkStatus.data.length === 0) {
                app.showWarning("请选择一笔数据进行操作");
                return;
            }
            if(checkStatus.data.length > 1) {
                app.showWarning("只能选择一笔数据进行操作");
                return;
            }
            let data = checkStatus.data[0];
            if(data.GWGCACTI != 'T') {
                if(checkStatus.data.length > 1) {
                    app.showWarning("已提交的资料才能查看审批流程");
                    return;
                }
            }

            self._openTplFull({
                tag: 'checkApprovalProcess',
                tagData: {
                    obj: {
                        fd_test_id:"16ebfe428516a8b4183af34442990e63",
                        fd_formal_id:"16f022ae2ddbb07eb9f82274e0180da0",
                        fd_process_id:data.GWGC021
                    }
                },
                index: 2,
                openOpts: {
                    title: "审批流程",
                    area: ['800px','400px']
                }
            });
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: vueData,
                    searchId: 'gwgi002',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if(field == 'GWGC001'){ //招标编号
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwgc001',
                                            columnList: [
                                                {label: '招标编号', prop: 'GWGC001', width: '150'},
                                                {label: '项目名称', prop: 'GWGC022', width: '150'},
                                                {label: '客户', prop: 'GWGC023_NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWGC001 = data.GWGC001;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择招标信息',
                                        area: ['600px', '465px']
                                    }
                                });
                            }else if (field == 'GWGA008') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWGA008 = data.GWAK001;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'GWGC005_NAME') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwak001',
                                            columnList: [
                                                {label: '代理商编号', prop: 'GWAK001', width: '150'},
                                                {label: '代理商简称', prop: 'GWAKL003', width: '150'},
                                                {label: '代理商全称', prop: 'GWAKL004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWGC005_NAME = data.GWAK001;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择代理商',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if(field == 'GWGCORIU'){
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysuser_id',
                                            columnList: [
                                                {label: '账号', prop: 'ACCOUNT', width: '150'},
                                                {label: '姓名', prop: 'NAME', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWGCORIU = data.ID;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择创建人',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initHeadTable();
            self.initGwgdTable();
            self.initGwgeTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwgi002();

});

